//
//  UITextField+PassWord.m
//  wangzhe
//
//  Created by 李 佳伟 on 2018/4/2.
//  Copyright © 2018年 李 佳伟. All rights reserved.
//

#import "UITextField+PassWord.h"
static CGFloat imagesize = 20;
@implementation UITextField (PassWord)

- (void)setTextFieldWithFont:(CGFloat)font color:(UIColor *)color alignment:(NSTextAlignment)alignment title:(NSString *)title placeHolder:(NSString *)placeholder isSelect:(BOOL)isSelect{
    
    UIButton *rightImageV = [[UIButton alloc] init];
    self.secureTextEntry = !isSelect;
    [rightImageV setBackgroundImage:[UIImage imageNamed:@"闭眼.png"] forState:UIControlStateNormal];
    [rightImageV setBackgroundImage:[UIImage imageNamed:@"睁眼睛-.png"] forState:UIControlStateSelected];
    rightImageV.frame = CGRectMake(0, 0, imagesize, imagesize);
    rightImageV.center =CGPointMake(self.frame.size.width-imagesize, self.frame.size.height/2);
    self.rightView = rightImageV;
    self.rightViewMode = UITextFieldViewModeAlways;
    [rightImageV addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
    self.font = [UIFont systemFontOfSize:font];
    self.textColor = color == nil ? [UIColor blackColor] : color;
    self.textAlignment = alignment;
    self.borderStyle = UITextBorderStyleNone;
    self.text = title == nil ? @"" : title;
    self.placeholder = placeholder == nil ? @"" : placeholder;
    rightImageV.selected = isSelect;
}
//监听右边按钮的点击,切换密码输入明暗文状态
-(void)btnClick:(UIButton *)btn{
    [self resignFirstResponder];//取消第一响应者
    btn.selected = !btn.selected;
    if (!btn.selected) {
        self.font = [UIFont systemFontOfSize:16];

        self.secureTextEntry = YES;
    }else{
        self.font = [UIFont systemFontOfSize:16];
        
        self.secureTextEntry = NO;
    }
    [self becomeFirstResponder];//放弃第一响应者
}
@end
